﻿using DongFangGuoKai.WCSPlatform.WebApi.Extensions;
using DongFangGuoKai.WCSPlatform.WebApi.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S7.Net;

namespace DongFangGuoKai.WCSPlatform.WebApi.Controllers
{
    /// <summary>
    /// 任务
    /// </summary>
    [Route("api/[controller]/[action]"), ApiController]
    public class PlcTaskController : ControllerBase
    {
        private readonly PlcTaskManager taskManager;
        private readonly Plc plc;
        private readonly PlcDBAddress plcDBAddress;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="taskManager"></param>
        /// <param name="plc"></param>
        /// <param name="plcDBAddress"></param>
        public PlcTaskController(PlcTaskManager taskManager,Plc plc,PlcDBAddress plcDBAddress)
        {
            this.taskManager = taskManager;
            this.plc = plc;
            this.plcDBAddress = plcDBAddress;
        }
        /// <summary>
        /// 获取入库任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<WCSApiResponse> GetInTaskList()
        {
            try
            {
                var taskRes = taskManager.GetInTaskDic();
                return this.Success(taskRes);
            }
            catch (Exception ex)
            {
                return this.Fail(ex.Message);
            }
        }
        /// <summary>
        /// 获取出库任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<WCSApiResponse> GetOutTaskList()
        {
            try
            {
                var taskRes = taskManager.GetOutTaskDic();
                return this.Success(taskRes);
            }
            catch (Exception ex)
            {
                return this.Fail(ex.Message);
            }
        }
        /// <summary>
        /// 删除入库任务
        /// </summary>
        /// <param name="taskCode">可空 传了之后会删除指定任务</param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult<WCSApiResponse> DelInTaskByTask(ushort? taskCode)
        {
            try
            {
                var taskRes = taskManager.DelInTaskDic(taskCode);
                return this.Success(taskRes);
            }
            catch (Exception ex)
            {
                return this.Fail(ex.Message);
            }
        }
        /// <summary>
        /// 删除出库任务
        /// </summary>
        /// <param name="taskCode">可空 传了之后会删除指定任务</param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult<WCSApiResponse> DelOutTaskByTask(ushort? taskCode)
        {
            try
            {
                var taskRes = taskManager.DelOutTaskDic(taskCode);
                return this.Success(taskRes);
            }
            catch (Exception ex)
            {
                return this.Fail(ex.Message);
            }
        }

        /// <summary>
        /// 手动入库
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<WCSApiResponse> ManualInTask(string palletCode)
        {
            try
            {
                taskManager.ManualInDoWork(palletCode);
                return this.Success();
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 手动出库
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<WCSApiResponse> ManualOutTask(string palletCode, ushort winCode)
        {
            try
            {
                taskManager.ManualOutDoWork(palletCode, winCode);
                return this.Success();
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 入库条码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<WCSApiResponse> ReadInPalletCode()
        {
            var palletCode = plc.ReadString(plcDBAddress.DBNumber, plcDBAddress.InPalletCode).HexToStr(); //入库箱子号
            return this.Success(data: palletCode);
        }
        /// <summary>
        /// 出库条码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<WCSApiResponse> ReadOutPalletCode()
        {
            var palletCode = plc.ReadString(plcDBAddress.DBNumber, plcDBAddress.OutPalletCode).HexToStr(); //入库箱子号
            return this.Success(data: palletCode);
        }
    }
}
